package org.jeecgframework.codegenerate.generate.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jeecgframework.codegenerate.config.DbConfig;
import org.jeecgframework.codegenerate.database.DbReadTableUtil;
import org.jeecgframework.codegenerate.generate.IGenerate;
import org.jeecgframework.codegenerate.generate.TemplateConfig;
import org.jeecgframework.codegenerate.generate.pojo.ColumnVo;
import org.jeecgframework.codegenerate.generate.pojo.TableVo;
import org.jeecgframework.codegenerate.util.NonceUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hutool.core.util.StrUtil;

public class CodeGenerateOne extends CodeGenerate implements IGenerate {
 private static final Logger logger = LoggerFactory.getLogger(CodeGenerateOne.class);
 private TableVo tableVo;
 private List<ColumnVo> columns;
 private List<ColumnVo> originalColumns;

 public CodeGenerateOne(TableVo tableVo) {
     this.tableVo = tableVo;
 }

 public CodeGenerateOne(TableVo tableVo, List<ColumnVo> columns, List<ColumnVo> originalColumns) {
     this.tableVo = tableVo;
     this.columns = columns;
     this.originalColumns = originalColumns;
 }

 public Map<String, Object> a() throws Exception {
     Map<String, Object> var1 = new HashMap();
     var1.put("bussiPackage", DbConfig.bussiPackage);
     var1.put("entityPackage", this.tableVo.getEntityPackage());
     var1.put("entityPackagePath", this.tableVo.getEntityPackage().replace(".", "/"));
     var1.put("entityName", this.tableVo.getEntityName());
     var1.put("tableName", this.tableVo.getTableName());
     var1.put("primaryKeyField", DbConfig.dbTableId);
     if (this.tableVo.getFieldRequiredNum() == null) {
         this.tableVo.setFieldRequiredNum(StringUtils.isNotEmpty(DbConfig.fieldRequiredNum) ? Integer.parseInt(DbConfig.fieldRequiredNum) : -1);
     }

     if (this.tableVo.getSearchFieldNum() == null) {
         this.tableVo.setSearchFieldNum(StringUtils.isNotEmpty(DbConfig.pageSearchFiledNum) ? Integer.parseInt(DbConfig.pageSearchFiledNum) : -1);
     }

     if (this.tableVo.getFieldRowNum() == null) {
         this.tableVo.setFieldRowNum(Integer.parseInt(DbConfig.fieldRowNum));
     }

     var1.put("tableVo", this.tableVo);

     try {
         if (this.columns == null || this.columns.size() == 0) {
             this.columns = DbReadTableUtil.a(this.tableVo.getTableName());
         }

         var1.put("columns", this.columns);
         if (this.originalColumns == null || this.originalColumns.size() == 0) {
             this.originalColumns = DbReadTableUtil.readOriginalTableColumn(this.tableVo.getTableName());
         }

         var1.put("originalColumns", this.originalColumns);
         Iterator var2 = this.originalColumns.iterator();

         while(var2.hasNext()) {
             ColumnVo var3 = (ColumnVo)var2.next();
             if (var3.getFieldName().toLowerCase().equals(DbConfig.dbTableId.toLowerCase())) {
                 var1.put("primaryKeyPolicy", var3.getFieldType());
             }
         }
     } catch (Exception var4) {
         throw var4;
     }

     long var5 = NonceUtils.c() + NonceUtils.h();
     var1.put("serialVersionUID", String.valueOf(var5));
     logger.info("load template data: " + var1.toString());
     return var1;
 }

 public List<String> generateCodeFile(String stylePath) throws Exception {
     //String var2 = org.jeecgframework.codegenerate.a.a.f;
     Map var3 = this.a();
     String templatepath = DbConfig.templatepath;
     if (a(templatepath, "/").equals("jeecg/code-template")) {
    	 templatepath = "/" + a(templatepath, "/") + "/one";
    	 DbConfig.setTemplatepath(templatepath);
     }

     TemplateConfig templateConfig = new TemplateConfig(templatepath);
     templateConfig.setStylePath(stylePath);
     if (this.tableVo != null && this.tableVo.getExtendParams() != null) {
    	 String vueStyle = (String)this.tableVo.getExtendParams().get("vueStyle");
    	 templateConfig.setVueStyle(StrUtil.isEmpty(vueStyle)? "vue" : vueStyle.trim());
     }

     this.loadTemplates(templateConfig, DbConfig.projectPath, var3);
     logger.info(" ----- jeecg-boot ---- generate  code  success =======> 表名：" + this.tableVo.getTableName() + " ");
     return this.msgs;
 }

 public List<String> generateCodeFile(String projectPath, String templatePath, String stylePath) throws Exception {
     if (projectPath != null && !"".equals(projectPath)) {
    	 DbConfig.setProjectPath(projectPath);
     }

     if (templatePath != null && !"".equals(templatePath)) {
    	 DbConfig.setTemplatepath(templatePath);
     }

     this.generateCodeFile(stylePath);
     return this.msgs;
 }

 public static void main(String[] args) {
     TableVo var1 = new TableVo();
     var1.setTableName("demo");
     var1.setPrimaryKeyPolicy("uuid");
     var1.setEntityPackage("test");
     var1.setEntityName("JeecgDemo");
     var1.setFtlDescription("jeecg 测试demo");

     try {
         (new CodeGenerateOne(var1)).generateCodeFile((String)null);
     } catch (Exception var3) {
         var3.printStackTrace();
     }

 }
}
